<template>
	<view v-if="showInfo">
		<view class="section">
			<view class="line"></view>
			<view class="content">
				<text>{{ measureUnit.catName }}</text>
				<text @click="synchronize('cpis', measureUnit)">{{ $t('app.my.synchronize') }}</text>
			</view>
		</view>
		<view class="space-all">
			<uni-table border stripe>
				<uni-tr>
					<uni-th align="center">{{ $t('app.my.valueCode') }}</uni-th>
					<uni-th align="center">{{ $t('app.my.valueName') }}</uni-th>
				</uni-tr>
				<uni-tr v-for="item in measureUnit.codeList" :key="item.id">
					<uni-td align="center">{{ item.key }}</uni-td>
					<uni-td align="center">{{ item.name}}</uni-td>
				</uni-tr>
			</uni-table>
		</view>
		
		<view class="section">
			<view class="line"></view>
			<view class="content">
				<text>{{ majorList.catName }}</text>
				<text @click="synchronize('cpis', majorList)">{{ $t('app.my.synchronize') }}</text>
			</view>
		</view>
		<view class="space-all">
			<uni-table  border stripe>
				<uni-tr>
					<uni-th align="center">{{ $t('app.my.valueCode') }}</uni-th>
					<uni-th align="center">{{ $t('app.my.valueName') }}</uni-th>
				</uni-tr>
				<uni-tr v-for="item in majorList.codeList" :key="item.id">
					<uni-td align="center">{{ item.key }}</uni-td>
					<uni-td align="center">{{ item.name}}</uni-td>
				</uni-tr>
			</uni-table>
		</view>
		
		<view class="section">
			<view class="line"></view>
			<view class="content">
				<text>{{ dfTypeList.catName }}</text>
				<text @click="synchronize('cpis', dfTypeList)">{{ $t('app.my.synchronize') }}</text>
			</view>
		</view>
		<view class="space-all">
			<uni-table  border stripe>
				<uni-tr>
					<uni-th align="center">{{ $t('app.my.valueCode') }}</uni-th>
					<uni-th align="center">{{ $t('app.my.valueName') }}</uni-th>
				</uni-tr>
				<uni-tr v-for="item in dfTypeList.codeList" :key="item.id">
					<uni-td align="center">{{ item.key }}</uni-td>
					<uni-td align="center">{{ item.name}}</uni-td>
				</uni-tr>
			</uni-table>
		</view>
	</view>
</template>

<script>
	import { findByTypeKeyExtend } from '@/api/platform/common.js'
	import { notice } from '@/utils/common.js'
	import * as sqliteUtil from '@/utils/sqlite.js'
	
	export default {
		data() {
			return {
				showInfo: false,
				measureUnit: {},
				majorList: {},
				dfTypeList: {},
				mpTypeList: {}
			}
		},
		onLoad() {
			this.init()
		},
		methods: {
			async init() {
				await findByTypeKeyExtend("jldw,zybm,qxlx,sbjscslx").then(res => {
					this.measureUnit.catNo = 'jldw'
					this.measureUnit.catName = this.$t('app.my.measuringUnit')
					this.measureUnit.codeList = res.data && res.data.jldw ? res.data.jldw : []
					this.measureUnit.codeList.forEach(item=>{
						item.typeKey = 'jldw'
					})
					
					this.majorList.catNo = 'zybm'
					this.majorList.catName = this.$t('app.my.majorCode')
					this.majorList.codeList = res.data && res.data.zybm ? res.data.zybm : []
					this.majorList.codeList.forEach(item=>{
						item.typeKey = 'zybm'
					})
					
					this.dfTypeList.catNo = 'qxlx'
					this.dfTypeList.catName = this.$t('defect.defInfo.dfType')
					this.dfTypeList.codeList = res.data && res.data.qxlx ? res.data.qxlx : []
					this.dfTypeList.codeList.forEach(item=>{
						item.typeKey = 'qxlx'
					})
					
					this.mpTypeList.catNo = 'sbjscslx'
					this.mpTypeList.catName = this.$t('app.my.mpType')
					this.mpTypeList.codeList = res.data && res.data.sbjscslx ? res.data.sbjscslx : []
					this.mpTypeList.codeList.forEach(item=>{
						item.typeKey = 'sbjscslx'
					})
				})
				this.showInfo = true
			},
			async synchronize(type, item) {
				if(!sqliteUtil.isOpen()) {
					await sqliteUtil.openSqlite()
				}
				
				if(type == 'cpis') {
					await sqliteUtil.createMCpisCatDic()
					// 先删除再插入，防止重复数据
					await sqliteUtil.deleteTableInfo('m_cpis_cat_dic', 'type_key', item.catNo)
					sqliteUtil.insertMCpisCatDicBatch(item.codeList)
					// item.codeList.forEach(code=>{
					// 	sqliteUtil.insertMCpisCatDic(code)
					// })
				}
				setTimeout(() => {
					notice.toast("同步成功")
				}, 500)
			}
		}
	}
</script>

<style lang="scss">
	.section {
		display: flex;
		align-items: center;
		margin: 10px 0;
		
		.line {
			width: 5px;
			height: 20px;
			margin: 0 10px;
			background-color: #1E91E6;
		}
		
		.content {
			display: flex;
			justify-content: space-between;
			width: 100%;
			margin-right: 10px;
		}
	}
	
	.space-all {
		padding: 10px;
	}
</style>
